package sxt.main;

import sxt.util.GameUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 箱子类
 */

public class GameBox extends GameCheckpoint {
    //矩形
    public Rectangle rect;
    //存放矩形集合
    public List<Rectangle> rects;
    //0代表为空 1代表围墙2蓝色砖块3黑砖快4黄砖块

//用于解决刷新问题
    boolean chenum=true;
    //用于记录成功个数
    int blk=0;
    int blkl=0;
    //关卡控制
    public int checkpoint=1;
    //定义图片
    private Image fire;
    private Image blue;
    private Image black;
    private Image yellow;
    //存入图片
    private static final String FIRE = "img/fire.jpg";
    private static final String BLUE = "img/blue.jpg";
    private static final String YELLOW = "img/yellow.jpg";
    private static final String BLACK = "img/black.jpg";

    //构造器初始化数据
    public GameBox() {
        fire = GameUtil.loadBufferedImage(FIRE);
        blue = GameUtil.loadBufferedImage(BLUE);
        black = GameUtil.loadBufferedImage(BLACK);
        yellow = GameUtil.loadBufferedImage(YELLOW);
        rect = new Rectangle();
        rects=new ArrayList<Rectangle>();
    }
    //箱子的位置
    int x=0,y=0;
    int box[][];
    /**
     * 绘制箱子
     */
    public void draw(Graphics g,GamePerson gamePerson,int check) {
        if(chenum){
        box=che(check);
        chenum=false;
        }
        for(int i=0;i<box.length;i++) {
            for(int j=0;j<box[0].length;j++) {
                switch(box[i][j]) {
                    case 0:
                        x=(j+1)*50;
                        y=50+i*50;
                        break;
                    case 1:
                        x=(j+1)*50;
                        y=50+i*50;
                        g.drawImage(fire,x,y,null);
                        save(x,y,g);
                        break;
                    case 2:
                        x=(j+1)*50;
                        y=50+i*50;
                        g.drawImage(yellow,x,y,null);
                        break;
                    case 3:
                        x=(j+1)*50;
                        y=50+i*50;
                        g.drawImage(black,x,y,null);
                        break;
                    case 4:
                        x=(j+1)*50;
                        y=50+i*50;
                        if (gamePerson.x == x && gamePerson.y == y) {
                                    if(gamePerson.up){
                                        if(box[i-1][j]!=1){
                                            if(box[i-1][j]==3){
                                                blkl++;
                                            }
                                            box[i][j]=2;
                                            box[i-1][j]=4;}
                                        else{
                                            gamePerson.y+=50;
                                        }

                                    }else if(gamePerson.down){
                                        if(box[i+1][j]!=1){
                                            if(box[i+1][j]==3){
                                                blkl++;
                                            }
                                            box[i][j]=2;
                                            box[i+1][j]=4;}
                                        else {
                                            gamePerson.y-=50;
                                        }
                                    }else if(gamePerson.right){
                                        if(box[i][j+1]!=1){
                                            if(box[i][j+1]==3){
                                                blkl++;
                                            }
                                            box[i][j]=2;
                                            box[i][j+1]=4;}
                                        else {
                                            gamePerson.x-=50;
                                        }
                                    }else if(gamePerson.left){
                                        if(box[i][j-1]!=1){
                                            if(box[i][j-1]==3){
                                                blkl++;
                                            }
                                        box[i][j]=2;
                                        box[i][j-1]=4;}
                                        else {
                                            gamePerson.x+=50;
                                        }
                                        }
                                    }
                        g.drawImage(blue,x,y,null);
                        break;

                }
            }
        }
        coll(gamePerson);
        if(blk==blkl && checkpoint==1){
            checkpoint = 2;
            chenum=true;
        }
    }
    /**
     * 绘制矩形的方法
     */
    public void save(int x,int y,Graphics g) {
        if(filesize>0){
        GameBox gameBox = new GameBox();
        gameBox.rect.x=x;
        gameBox.rect.y=y;
        gameBox.rect.width=50;
        gameBox.rect.height=50;
        rects.add(gameBox.rect);
        filesize--;
        }
        g.setColor(Color.red);
        g.drawRect(rect.x,rect.y,rect.width,rect.height);
    }
    int filesize=0 ;
    public int[][] che(int check){
        switch(check) {
            case 1:
                for (int i = 0; i < box1.length; i++) {
                    for (int j = 0; j < box1[0].length; j++) {
                        if (box1[i][j] == 1) {
                            filesize++;
                        }
                        if (box1[i][j] == 3) {
                            blk++;
                        }
                    }
                }
                return box1;
            case 2:
                rects.clear();
                for (int i = 0; i < box1.length; i++) {
                    for (int j = 0; j < box1[0].length; j++) {
                        if (box1[i][j] == 1) {
                            filesize++;
                        }
                        if (box1[i][j] == 3) {
                            blk++;
                        }
                    }
                }
                return box2;


        }
    return null;
}


    /**
     * 墙体与小人发生碰撞方法
     */
    public void coll(GamePerson gamePerson) {
        for(int i=0;i<rects.size();i++) {
           Rectangle rectangle=rects.get(i);
            //判断矩形是否相交
            if (gamePerson.rect.intersects(rectangle)){
                System.out.println("撞上了");
                if(gamePerson.up){
                    gamePerson.y+=50;
                }else if(gamePerson.down){
                    gamePerson.y-=50;
                }else if(gamePerson.right){
                    gamePerson.x-=50;
                }else if(gamePerson.left){
                    gamePerson.x+=50;
                }
            }
        }

    }
}
